package com.esun.util;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.util.log.EDebug;
import com.esun.util.md5.MD5;

public class LoginTimeControl {
	long loginTime;
	long durationTime;

	public boolean isNeedInputPwd() {

		loginTime = UserInfo.getUserInfo().getLogintime();
		durationTime = System.currentTimeMillis() - loginTime;
		EDebug.o("logintime----------" + loginTime);
		EDebug.o("durationTime-------" + durationTime);
		EDebug.o("UserInfo.getUserInfo().getDurationTime()-----"
				+ UserInfo.getUserInfo().getDurationTime());
		if (durationTime > UserInfo.getUserInfo().getDurationTime()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 用户账户保护密码输入框
	 * 
	 * @param context
	 * @param btChooseTime
	 * @param cbProAccount
	 */

	public static void createPassDialog(final Context context,
			final Button btChooseTime, final CheckBox cbProAccount) {
		final Dialog inputPwdDialog = new Dialog(context, R.style.dialog);
		inputPwdDialog.setCancelable(false);
		inputPwdDialog.getWindow().setContentView(
				R.layout.account_protect_dialog);
		final Button btClear = (Button) inputPwdDialog.getWindow()
				.findViewById(R.id.btClear);
		TextView tvUserName = (TextView) inputPwdDialog.getWindow()
				.findViewById(R.id.tvUserName);
		tvUserName.setText(context.getText(R.string.input_pwd) + ":"
				+ UserInfo.getUserInfo().getUserName());
		final AutoCompleteTextView tvPassWord = (AutoCompleteTextView) inputPwdDialog
				.getWindow().findViewById(R.id.edtPawssWord);
		tvPassWord.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				btClear.setVisibility(View.VISIBLE);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		btClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tvPassWord.setText("");
				btClear.setVisibility(View.INVISIBLE);
			}
		});
		Button btSure = (Button) inputPwdDialog.getWindow().findViewById(
				R.id.btSure);
		Button btCancel = (Button) inputPwdDialog.getWindow().findViewById(
				R.id.btCancel);
		btSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String input = MD5.md5(tvPassWord.getText().toString());
				SharedPreferences sharedPreferences_in = context
						.getSharedPreferences("cheetah", Context.MODE_PRIVATE);
				String passWor = sharedPreferences_in.getString(
						Key.SAVE_PASSWORD, "");
				if (btChooseTime != null) {
					btChooseTime.setBackgroundDrawable(context.getResources()
							.getDrawable(
									R.drawable.btn_disable_customtime_32x32));
				}

				if (!input.equals(passWor)) {
					Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
					tvPassWord.setText("");
					if (btChooseTime != null) {
						btChooseTime.setBackgroundDrawable(context
								.getResources().getDrawable(
										R.drawable.login_icon_default_19x19));
					}

					return;
				}
				context.getSharedPreferences(Config.SHARED_PREFERENCE_NAME,
						Context.MODE_PRIVATE).edit()
						.putBoolean(Config.ACCOUNT_ISON, false).commit();
				inputPwdDialog.cancel();
			}
		});
		btCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inputPwdDialog.cancel();
				if (cbProAccount != null) {
					cbProAccount.setChecked(true);
				}

			}
		});
		inputPwdDialog.show();
	}
}
